<template>
  <div class="login_container">
    <div class="box">
      <div class="avater">
        <img src="@/assets/logo.png" alt="">
      </div>
      <el-form :model="form" ref="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type='password' v-model="form.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit('form')">登陆</el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script>
// import {login} from '@/api/login'
export default {
  name: 'login',
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ]
      }
    }
  },
  methods: {
    onSubmit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log('submit!');
          const data = Object.assign({}, this.form)
          this.$http.post('/login', data).then((res) => {
            // login(data).then((res) => {
            res = res.data;
            console.log(res)
            if (res.meta.status == 200) {
              sessionStorage.token = res.data.token
              this.$message({
                type: 'success', message: res.meta.msg, time: 800, onClose: () => {
                  this.$router.push('/home')
                }
              })
            } else {
              this.$message({ type: 'error', message: res.meta.msg })
            }
          }).catch((err) => {
            console.log(err)
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background: #ddd;
  height: 100%;
  overflow: hidden;
  .box {
    width: 450px;
    height: 320px;
    background: #fff;
    border-radius: 5px;
    margin: auto;
    transform: translateY(50%);
  }
  .avater {
    text-align: center;
    border: 2px solid #eee;
    width: 80px;
    height: 80px;
    box-shadow: 0 0 15px #ddd;
    border-radius: 100%;
    margin: 0 auto;
    transform: translateY(-50%);
    background: #fff;
    padding: 10px;
    img {
      width: 100%;
      height: 100%;
      border-radius: 100%;
      background: #ddd;
    }
  }
}
</style>